﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ignitron.EEngine.Maths;

namespace OpenTKLightingTool.Controls
{
    /// <summary>
    /// Interaction logic for Vector4Control.xaml
    /// </summary>
    public partial class Vector4Control : UserControl
    {
        public static DependencyProperty IsColorProperty;
        public static DependencyProperty MinValueProperty;
        public static DependencyProperty MaxValueProperty;

        /// <summary>
        /// Registers the dependency property
        /// </summary>
        static Vector4Control()
        {
            IsColorProperty = DependencyProperty.Register("IsColor", typeof(bool), typeof(Vector4Control),
                                    new PropertyMetadata(false));
            MinValueProperty = DependencyProperty.Register("MinValue", typeof(float), typeof(Vector4Control),
                                    new PropertyMetadata(0f));
            MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(float), typeof(Vector4Control),
                                            new PropertyMetadata(1f));
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public Vector4Control()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Dependency property wrapper
        /// </summary>
        public bool IsColor
        {
            get { return (bool)GetValue(IsColorProperty); }
            set { SetValue(IsColorProperty, value); }
        }

        /// <summary>
        /// Dependency property wrapper
        /// </summary>
        public float MinValue
        {
            get { return (float)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }

        /// <summary>
        /// Dependency property wrapper
        /// </summary>
        public float MaxValue
        {
            get { return (float)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }

        /// <summary>
        /// Returns vector value
        /// </summary>
        public EVector4 Value
        {
            get
            {
                return new EVector4(uxX.Value, uxY.Value, uxZ.Value, uxW.Value);
            }
            set
            {
                uxX.Value = value.X;
                uxY.Value = value.Y;
                uxZ.Value = value.Z;
                uxW.Value = value.W;
            }
        }

        /// <summary>
        /// Notification when a dependency property has changed
        /// </summary>
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.Property == IsColorProperty)
            {
                if ((bool)e.NewValue == true)
                {
                    uxLabelX.Content = "R:";
                    uxLabelY.Content = "G:";
                    uxLabelZ.Content = "B:";
                    uxLabelW.Content = "A:";
                }
                else
                {
                    uxLabelX.Content = "X:";
                    uxLabelY.Content = "Y:";
                    uxLabelZ.Content = "Z:";
                    uxLabelW.Content = "W:";
                }
            }
            else if (e.Property == MinValueProperty)
            {
                float value = (float)e.NewValue;
                uxX.MinValue = value;
                uxY.MinValue = value;
                uxZ.MinValue = value;
                uxW.MinValue = value;
            }
            else if (e.Property == MaxValueProperty)
            {
                float value = (float)e.NewValue;
                uxX.MaxValue = value;
                uxY.MaxValue = value;
                uxZ.MaxValue = value;
                uxW.MaxValue = value;
            }

            base.OnPropertyChanged(e);
        }
    }
}
